﻿using System.Collections.Generic;
using System.Runtime.InteropServices.ComTypes;

namespace MoBaoPay.Model.Request
{
    /// <summary>
    /// 微信支付请求参数
    /// </summary>
    internal class MoBaoWechatPayRequest : IMoBaoRequest
    {
        /// <summary>
        /// 接口名称
        /// </summary>
        public string ApiName { get; set; } = "WECHAT";

        /// <summary>
        /// 商户号
        /// </summary>
        public string CustNo { get; set; }

        /// <summary>
        /// 商户订单号
        /// </summary>
        public string MerchTradeNo { get; set; }

        /// <summary>
        /// 支付金额，保留两位小数
        /// </summary>
        public decimal TradeAmt { get; set; }

        /// <summary>
        /// 商品名称
        /// </summary>
        public string GoodsName { get; set; }

        /// <summary>
        /// 商品描述
        /// </summary>
        public string GoodsDesc { get; set; }

        /// <summary>
        /// 商户订单创建时间
        /// </summary>
        public string TradeTime { get; set; }

        /// <summary>
        /// 支付回调地址
        /// </summary>
        public string MerchSyncUrl { get; set; }

        /// <summary>
        /// 后台通知地址
        /// </summary>
        public string MerchUrl { get; set; }

        /// <summary>
        /// 支付环境
        /// </summary>
        public int PayEnvType { get; set; } = 1;

        /// <summary>
        /// 支付方式
        /// </summary>
        public int PayType { get; set; } = 162;

        /// <summary>
        /// 交易摘要，对支付的简单说明。商品名称|商品数量
        /// </summary>
        public string TradeSummer { get; set; }

        /// <summary>
        /// 产品类型
        /// 120:扫码支付
        /// 160:微信支付
        /// </summary>
        public int Product { get; set; } = 160;

        /// <summary>
        /// 交易来源
        /// </summary>
        public int TerminalType { get; set; } = 1;

        /// <summary>
        /// 消费者IP
        /// </summary>
        public string TerminalId { get; set; }

        /// <summary>
        /// OpenID
        /// </summary>
        public string OpenId { get; set; }

        /// <summary>
        /// AppId
        /// </summary>
        public string AppId { get; set; }

        /// <summary>
        /// 设备号
        /// </summary>
        public string DeviceNo { get; set; } = "WEB";

        /// <summary>
        /// 签名
        /// </summary>
        public string SignMsg { get; set; }

        /// <summary>
        /// 微信支付请求地址
        /// </summary>
        private string requestUrl = "https://trade2.mobaopay.com/cashier/api/gateway.cgi";

        public string GetRequestUrl()
        {
            return requestUrl;
        }

        public void SetRequestUrl(string url)
        {
            requestUrl = url;
        }

        public IDictionary<string, string> GetRequestContent()
        {
            return new Dictionary<string, string>
            {
                { "apiName", ApiName },
                { "custNo", CustNo },
                { "merchTradeNo", MerchTradeNo },
                { "tradeAmt", TradeAmt.ToString("F2") },
                { "goodsName", GoodsName },
                { "goodsDesc", GoodsDesc },
                { "tradeTime", TradeTime },
                { "merchSyncUrl", MerchSyncUrl },
                { "merchUrl", MerchUrl },
                { "payEnvType", PayEnvType.ToString() },
                { "payType", PayType.ToString() },
                { "tradeSummer", TradeSummer },
                { "product", Product.ToString() },
                { "terminalType", TerminalType.ToString() },
                { "terminalId", TerminalId },
                { "openId", OpenId },
                { "appId", AppId },
                { "deviceNo", DeviceNo },
                { "signMsg", SignMsg }
            };
        }

        public void Sign(string path, string password)
        {
            var strData =
                $"apiName={ApiName}&appId={AppId}&custNo={CustNo}&deviceNo={DeviceNo}&goodsDesc={GoodsDesc}&goodsName={GoodsName}&merchSyncUrl={MerchSyncUrl}&merchTradeNo={MerchTradeNo}" +
                $"&merchUrl={MerchUrl}&openId={OpenId}&payEnvType={PayEnvType}&payType={PayType}" +
                $"&product={Product}&terminalId={TerminalId}&terminalType={TerminalType}&tradeAmt={TradeAmt:F2}&tradeSummer={TradeSummer}&tradeTime={TradeTime}";
            this.SignMsg = MD5WithRSA.SignData(path, password, strData);
        }
    }
}